﻿

BalloonShooter.Som = function (nomeArquivo, canais, loop) {
    this.canaisAudio = new Array();
    var extensao;
    var numeroCanais = canais;
    var testeSom = new Audio("");

    for (a = 0; a < numeroCanais; a++) {
        this.canaisAudio[a] = new Array();
        this.canaisAudio[a]['channel'] = new Audio();
        this.canaisAudio[a]['finished'] = -1;
    }

    if (testeSom.canPlayType('audio/ogg') != "")
        extensao = '.ogg';
    else if (testeSom.canPlayType('audio/wav') != "")
        extensao = '.wav';
    else if (testeSom.canPlayType('audio/mpeg') != "")
        extensao = '.mp3';
    else
        return;

    this.audio = new Audio(nomeArquivo + extensao);
    this.audio.duration = 0;
	
    this.audio.load();
}

BalloonShooter.Som.prototype.tocar = function () {
    var canaisAudio = this.canaisAudio;

    for (a = 0; a < canaisAudio.length; a++) {
        thistime = new Date();

        if (canaisAudio[a]['finished'] < thistime.getTime()) {
            canaisAudio[a]['finished'] = thistime.getTime() + this.audio.duration * 1000;
            canaisAudio[a]['channel'].src = this.audio.src;
            canaisAudio[a]['channel'].load();
            canaisAudio[a]['channel'].play();

            break;
        }
    }
}